package com.itheima.quartz;

import com.itheima.resource.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

@Component
public class QiniuyunCleanImageObject {
    @Autowired
    private JedisPool jedisPool;
    private  static int  count =1;

    public void run(){
        Jedis resource = jedisPool.getResource();
        //用于区分七牛云与数据库中的数据，
        Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff.size()>0){ //如果七牛云的图片比数据库的图片多，则清理七牛云
            List<String> imagefilenames = QiniuUtils.setToArrayList(sdiff);
            for (String imagefilename : imagefilenames) {
                QiniuUtils.deleteFileFromQiniu(imagefilename);
            }
            System.out.println("清理七牛云的图片"+imagefilenames+"===");
            //再清理redis缓存中的数据
            String[] clearImgFromRedis = imagefilenames.toArray(new String[]{});
            resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,clearImgFromRedis);

        }else {
            System.out.println("quartz no task ------no qiniuyun need clean   it is clean!!");
        }
        System.out.println("quartz------clean qiniuyun and red wuxiao image "+ count++ +" times  ok");
        resource.close();
    }
}
